package com.chuangke.lowcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chuangke.common.db.query.domain.Condition;
import com.chuangke.common.db.query.domain.Order;
import com.chuangke.common.db.query.domain.Page;
import com.chuangke.lowcode.model.EntityModel;

@SuppressWarnings("rawtypes")
public class LowCodeContext {

    /********************* 配置参数，需传入 **********************/
    //主模型，如果是多表，对应着表头
    private EntityModel entityModel;
    //业务表单Id，对应着sys_bill中的id，根据id去加载flowId ;
    private String formId;
    //流程id
    private String flowId;
    //流程服务(默认为DefaultOrderFlowServiceImpl)
    private Class<?> flowServiceClass;

    //模型数据
    private List datas;

    //流程字段和模型字段的映射，key为流程字段，value为实体数据字段
    private Map<String, String> flowModelFeildMapping = new HashMap<>();
    //流程变量字段
    private List<String> flowVarList = new ArrayList<>();

    //条件参数
    private List<Condition> conditions ;
    private List<Order> orders ;
    private Page page ;

    /********************** 程序自动处理参数 **********************/
    // 将datas组装成各个业务模型对应的数据，key为各个业务模型的id
    private Map<String, List> modelDatas;// key:model.id

    private QueryWrapper queryWrapper = new QueryWrapper();

    /********************** 程序处理结果集 **********************/
    private Object result;

    public EntityModel getEntityModel() {
        return entityModel;
    }

    public LowCodeContext setEntityModel(EntityModel model) {
        this.entityModel = model;
        return this;
    }

    public List getDatas() {
        return datas;
    }

    public LowCodeContext setDatas(List datas) {
        this.datas = datas;
        return this;
    }

    public String getFormId() {
        return formId;
    }

    public LowCodeContext setFormId(String formId) {
        this.formId = formId;
        return this;
    }

    public String getFlowId() {
        return flowId;
    }

    public LowCodeContext setFlowId(String flowId) {
        this.flowId = flowId;
        return this;
    }

    public List<Condition> getConditions() {
        return conditions;
    }

    public LowCodeContext setConditions(List<Condition> conditions) {
        this.conditions = conditions;
        return this;
    }

    public List<Order> getOrders() {
        return orders;
    }

    public LowCodeContext setOrders(List<Order> orders) {
        this.orders = orders;
        return this;
    }

    public Page getPage() {
        return page;
    }

    public LowCodeContext setPage(Page page) {
        this.page = page;
        return this;
    }

    public QueryWrapper getQueryWrapper() {
        return queryWrapper;
    }

    public void setQueryWrapper(QueryWrapper queryWrapper) {
        this.queryWrapper = queryWrapper;
    }

    public Map<String, String> getFlowModelFeildMapping() {
        return MapUtils.isEmpty(flowModelFeildMapping) ? getDefaultFlowModelFeildMapping() : flowModelFeildMapping;
    }

    public LowCodeContext setFlowModelFeildMapping(Map<String, String> flowModelFeildMapping) {
        this.flowModelFeildMapping = flowModelFeildMapping;
        return this;
    }

    public List<String> getFlowVarList() {
        return flowVarList;
    }

    public LowCodeContext setFlowVarList(List<String> flowVarList) {
        this.flowVarList = flowVarList;
        return this;
    }

    public Class<?> getFlowServiceClass() {
//        return flowServiceClass == null ? DefaultOrderFlowServiceImpl.class : flowServiceClass;
    	return flowServiceClass ;
    }

    public LowCodeContext setFlowServiceClass(Class<?> flowServiceClass) {
        this.flowServiceClass = flowServiceClass;
        return this;
    }


    public Map<String, List> getModelDatas() {
        return modelDatas;
    }

    public void setModelDatas(Map<String, List> modelDatas) {
        this.modelDatas = modelDatas;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public static Map<String, String> getDefaultFlowModelFeildMapping() {
        Map<String, String> flowModelFeildMapping = new HashMap<>();
        flowModelFeildMapping.put("orderId", "billId");
        flowModelFeildMapping.put("orderDate", "date");
        flowModelFeildMapping.put("orderType", "bisType");
        flowModelFeildMapping.put("orderRemarks", "remarks");
        flowModelFeildMapping.put("orderDeptId", "deptId");
        return flowModelFeildMapping;
    }

}
